<?php
namespace app\site\controller;

use app\common\controller\SiteBase;
use think\facade\Db;

class Module extends SiteBase
{
    public function index()
    {
        $param = $this->request->param();
        //$data = Db::name('site_web')->find($param['id']);
        // if(!empty($param['kw'])){
        //     $model = $model->where('title','like','%'.$param['kw'].'%');
        // }
        $module = Db::connect('dynamic')->table('destoon_module')->find($param['moduleid']);
        $table = 'destoon_'.$module['module'].'_'.$module['moduleid'];
        $list = Db::connect('dynamic')->table($table)->alias('a')->join('destoon_category b','a.catid=b.catid','left')->field('a.*,b.catname')->order('itemid desc')->paginate(['query' => $param]);
        $count = $list->total();
        return view('',['web'=>$this->web,'list'=>$list,'count'=>$count,'param'=>$param,'module'=>$module]);
    }

    public function add(){
        if( $this->request->isPost() ) {
            // $param = $this->request->param();
            // $result = DataAttrModel::create($param);
            // if( $result ) {
            //     xn_add_admin_log('添加属性');
            //     $this->success('操作成功');
            // } else {
            //     $this->error('操作失败');
            // }
        }
        $param = $this->request->param();
        $module = Db::connect('dynamic')->table('destoon_module')->find($param['moduleid']);
        $tb = $module['module'].'_'.$module['moduleid'];
        $categorylist = Db::connect('dynamic')->table('destoon_category')->where('moduleid',$param['moduleid'])->select()->toArray();
        $categorylist = formatTree($categorylist, 0, 1, $field = array('pid'=>'parentid','id'=>'catid','children'=>'children'));
        $fields = Db::connect('dynamic')->table('destoon_fields')->where('tb',$tb)->order('listorder asc')->select();
        return view('form',['web'=>$this->web,'param'=>$param,'categorylist'=>$categorylist,'fields'=>$fields,'module'=>$module]);
    }

    public function edit(){
        if( $this->request->isPost() ) {
            // $param = $this->request->param();
            // $result = DataAttrModel::update($param);
            // if( $result ) {
            //     xn_add_admin_log('修改属性');
            //     $this->success('操作成功');
            // } else {
            //     $this->error('操作失败');
            // }
        }
        $param = $this->request->param();
        $module = Db::connect('dynamic')->table('destoon_module')->find($param['moduleid']);
        $tb = $module['module'].'_'.$module['moduleid'];
        $tb_data = $module['module'].'_data_'.$module['moduleid'];
        $categorylist = Db::connect('dynamic')->table('destoon_category')->where('moduleid',$param['moduleid'])->select();
        $categorylist = formatTree($categorylist, 0, 1, $field = array('pid'=>'parentid','id'=>'catid','children'=>'children'));
        $categorylist = formatTree($categorylist, 0, 1, $field = array('pid'=>'parentid','id'=>'catid','children'=>'children'));
        $fields = Db::connect('dynamic')->table('destoon_fields')->where('tb',$tb)->order('listorder asc')->select();
        $data = Db::connect('dynamic')->table('destoon_'.$tb.' a')->join('destoon_'.$tb_data.' b','a.itemid=b.itemid','left')->field('a.*,b.content')->find($param['itemid']);
        return view('form',['web'=>$this->web,'param'=>$param,'categorylist'=>$categorylist,'fields'=>$fields,'data'=>$data,'module'=>$module]);
    }

    public function delete()
    {
        $id = intval($this->request->get('id'));
        !($id>0) && $this->error('参数错误');
        DataAttrModel::destroy($id);
        xn_add_admin_log('删除属性');
        $this->success('删除成功');
    }

    
    
    public function batchSubmit(){
        $list = Db::name('site_web')->select();
        return view('',['list'=>$list]);
    }
    

    private function getKeyword($xiaolei){
        $typeid = Db::name('data_type')->field('id')->where('title',$xiaolei)->find();
        $typeid = $typeid['id'];
        $res = Db::name('data_content_keyword')->field('title')->where('typeid',$typeid)->orderRand()->find();
        return $res['title'];
    }

    private function getSuffix(){
        $res = Db::name('data_content_suffix')->field('title')->orderRand()->find();
        return $res['title'];
    }

    private function getTransition(){
        $res = Db::name('data_content_transition')->field('title')->orderRand()->find();
        return $res['title'];
    }

    private function getPic(){
        $res = Db::name('data_content_pic')->field('url')->where('project','一点点奶茶')->orderRand()->find();
        return '<img src="'.$res['url'].'">';
    }

    private function getText($xiaolei){
        $typeid = Db::name('data_type')->field('id')->where('title',$xiaolei)->find();
        $typeid = $typeid['id'];
        $res = Db::name('data_content_grab')->field('content,project,title')->where('typeid',$typeid)->orderRand()->find();
        preg_match_all( '/<p>(.*)<\/p>/iU', $res['content'], $matches);
        if(empty($matches[0])){
            return $this->getText($xiaolei);
        }
        $start = '';
        $end = '';
        for($i=0; $i<count($matches[0]); $i++){
            $start .= $matches[0][$i];
            if(mb_strlen($start)>300){
                break;
            }
        }
        for($i=count($matches[0])-1; $i>=0; $i--){
            $end = $matches[0][$i].$end;
            if(mb_strlen($end)>300){
                break;
            }
        }
        $arr['start'] = $start;
        $arr['end'] = $end;
        return $arr;
    }

}